#!/bin/sh

set -e

TDIR=$1
MIRROR=$2
DISKNUM=$3
CDDIR=$4
ARCHES=$5

# Copy grub theme assets
# Until debian-cd picks them from d-i automatically:
# http://bugs.debian.org/1003927
cp -r $BASEDIR/../../lingmo-config/common/bootloaders/grub-pc/theme \
      $BASEDIR/../../lingmo-config/common/bootloaders/grub-pc/splash.png \
      $CDDIR/boot/grub/

cd $CDDIR

fix_branding() {
    sed -i -e 's|Debian GNU/Linux|Lingmo Linux|g' \
	-e 's|Lingmo GNU/Linux|Lingmo Linux|g' \
	-e 's|Debian lingmo-rolling|Lingmo Rolling|g' \
	-e 's|Debian|Lingmo|g' \
	-e 's|DEBIAN|LINGMO|g' \
	"$@"
}

# Rebrand Debian into Lingmo
if [ -e ../boot$DISKNUM/isolinux/menu.cfg ]; then
    fix_branding ../boot$DISKNUM/isolinux/menu.cfg \
		 ../boot$DISKNUM/isolinux/*.txt
fi
if [ -d boot/grub/theme ] && [ -s boot/grub/theme ]; then
    fix_branding boot/grub/theme/*
fi

# Remove Debian's windows loader
rm -f autorun.inf win32-loader.ini setup.exe

# Replace Debian specific documentation
rm -rf css
cat >README.txt <<EOF
This disc contains an installer for Lingmo Linux.

Read more at: https://www.lingmo.org
EOF
cat >README.html <<EOF
<html>
<head><title>Lingmo Linux Installer Disc</title></head>
<body>
This disc contains an installer for Lingmo Linux.

Read more at: <a href="https://www.lingmo.org">www.lingmo.org</a>
</body>
</html>
EOF

# Replace lingmo-last-snapshot with lingmo-rolling
if [ -e dists/lingmo-last-snapshot ]; then
    mv dists/lingmo-last-snapshot dists/lingmo-rolling
    rm -f dists/stable && ln -sf lingmo-rolling dists/stable
    sed -i -e 's|lingmo-last-snapshot|lingmo-rolling|g' \
	dists/lingmo-rolling/Release
fi

# Redo the md5sum.txt due to our changes
find . -type f | grep -v ./md5sum.txt | xargs md5sum | sort -uk2 > md5sum.txt
